var qn = require('qn');
var s = require('../config');
// qn
var client = qn.create({
  accessKey: s.qiniu.ACCESS_KEY,
  secretKey: s.qiniu.SECRET_KEY,
  bucket: s.qiniu.Bucket_Name,
  domain: s.qiniu.Domain
});
//服务器信息
var os = require('os');

var express = require('express');
var router = express.Router();

var news =  require('../services/news.service');

/* GET home page. */
router.get('/', function (req, res, next) {
  news.getParams();
  res.render('ueditor', { title: 'Express'});
});
router.post('/editor', function (req, res) {
  console.log(req.body);
  res.send('ok');
})
router.post('/case/add', function (req, res) {

  var imageData = req.body.imageData;
  var imageName = req.body.imageName;


  imageData = imageData.replace(/^data:image\/\w+;base64,/, "");
  var dataBuffer = new Buffer(imageData, 'base64');

  client.upload(dataBuffer, {

  }, function (err, result) {
    if (err) {
      res.json({
        state: false,
        imgname: imageName,
        imgurl: "",
        imghash: ""
      });
    } else {
      res.json({
        state: true,
        imgname: result.key,
        imgurl: result.url,
        imghash: result.hash
      });
    }
  });
})
module.exports = router;
